// pages/search/search.js
const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
const SETGOODS = require('../../utils/setgoos.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputVal:'',
    type:'',
    goodsList:[],
    orderList:[],
    curPage:1,
    loadingMoreHidden:true,
    showList:false,
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  // 清空输入框
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  // 去详情页
  toDetailsTap: function(e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  // 搜索
  toSearch(){
    if(this.data.inputVal.trim().length < 1){
      app.showLoadText('请输入搜索内容');
      return;
    }
    this.setData({
      curPage: 1,
      goodsList:[],
      orderList: [],
      showList:true,
    });
    if (this.data.type == 'searchGoods') {
      this.seachGoodsList();
    } else if (this.data.type == 'searchOrder') {
      this.seachOrderList();
    }
  },
  // 搜索单号
  seachOrderList(){

  },
  // 搜索商品
  seachGoodsList: function () {
    var that = this;
    wx.showLoading({
      "mask": true
    })
    WXAPI.goods({
      keyWords: that.data.inputVal,
      pageNum: this.data.curPage,
      pageSize: this.data.pageSize
    }).then(function (res) {
      wx.hideLoading()
      if (res.retCode !== '200') {
        that.setData({
          loadingMoreHidden: false
        });
        return
      }
      let goods = that.data.goodsList;
      for (var i = 0; i < res.data.list.length; i++) {
        goods.push(res.data.list[i]);
      }
      that.setData({
        goodsList: goods,
        loadingMoreHidden: true,
      });
    })
  },
  // 加购商品到购物车
  toShopCar(e) {
    let that = this;
    let itemNo = e.currentTarget.dataset.itemno;
    for (let item of that.data.goodsList) {
      // console.log(item)
      if (item.itemNo == itemNo) {
        item.buyNumber = 1;
        for (let [index, dots] of item.itemSkuResultDtos.entries()) {
          dots.checked = false;
          dots.value = index;
          dots.text = dots.stockTitle;
        }
        // 选中的规格
        item.itemSkuResultDtos[0].checked = true;
        item.value = 0;
        item.price = item.itemSkuResultDtos[0].price;
        // 购物车选中标识
        item.checked = false;
        item.buyMoney = parseFloat(item.price) * (item.buyNumber);
        item.stockNo = item.itemSkuResultDtos[0].stockNo;
        item.stockTitle = item.itemSkuResultDtos[0].stockTitle;
        item.narrowPicUrl = item.itemSkuResultDtos[0].narrowPicUrl;

        SETGOODS.setGoods(item, itemNo, item.itemSkuResultDtos[0].stockNo, item.shopNo);
      }
    }
    wx.showToast({
      title: '商品已添加购物车',
      duration: 1500,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    if (this.data.loadingMoreHidden){
      this.setData({
        curPage: this.data.curPage + 1
      })
      if (this.data.type == 'searchGoods') {
        this.seachGoodsList();
      } else if (this.data.type == 'searchOrder') {

      }
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})